﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;

/*
#################################################################################################################
#                                                                                                               #
#                                                Auto Auth-Code 说明                                            #
#                                    作者：陈粤雄  联系：Aresh@163.com 时间:2009-05-11                          #
#               欢迎使用本控件，你可以免费自由使用及转载本控件及所有源代码，但请保留本说明信息                  #
#                                                                                                               #
#################################################################################################################
*/
namespace HAOest.Web.UI.WebControls.AutoAuthCode
{
    /// <summary>
    /// 验证码图片样式
    /// </summary>
    [Serializable]
    public class ImageStyle
    {
        Color imgBgColor = Color.White;
        Color imgNoiseColor = Color.GreenYellow;
        Color imgBorderColor = Color.DeepSkyBlue;
        Color textColor1 = Color.DeepSkyBlue;
        Color textColor2 = Color.BlueViolet;
        int textFontSize = 17;
        Size imgSize = Size.Empty;


        /// <summary>
        /// 图片背景色
        /// </summary>
        [Bindable(false)]
        [Category("外观扩展"), Description("图片背景色"), NotifyParentProperty(true)]
        [DefaultValue("White")]
        [Localizable(false)]
        public Color ImgBgColor
        {
            get
            {
                return imgBgColor;
            }

            set
            {
                imgBgColor = value;
            }
        }

        /// <summary>
        /// 图片噪声颜色
        /// </summary>
        [Bindable(false)]
        [Category("外观扩展"), Description("图片噪声颜色"), NotifyParentProperty(true)]
        [DefaultValue("GreenYellow")]
        [Localizable(false)]
        public Color ImgNoiseColor
        {
            get
            {
                return imgNoiseColor;
            }

            set
            {
                imgNoiseColor = value;
            }
        }

        /// <summary>
        /// 图片边框颜色
        /// </summary>
        [Bindable(false)]
        [Category("外观扩展"), Description("图片边框颜色"), NotifyParentProperty(true)]
        [DefaultValue("DeepSkyBlue")]
        [Localizable(false)]
        public Color ImgBorderColor
        {
            get
            {
                return imgBorderColor;
            }

            set
            {
                imgBorderColor = value;
            }
        }

        /// <summary>
        /// 文本颜色
        /// </summary>
        [Bindable(false)]
        [Category("外观扩展"), Description("文本颜色1"), NotifyParentProperty(true)]
        [DefaultValue("DeepSkyBlue")]
        [Localizable(false)]
        public Color TextColor1
        {
            get
            {
                return textColor1;
            }

            set
            {
                textColor1 = value;
            }
        }

        /// <summary>
        /// 文本颜色2(如果文本为单色则无效)
        /// </summary>
        [Bindable(false)]
        [Category("外观扩展"), Description("文本颜色2(如果文本为单色则无效)"), NotifyParentProperty(true)]
        [DefaultValue("BlueViolet")]
        [Localizable(false)]
        public Color TextColor2
        {
            get
            {
                return textColor2;
            }

            set
            {
                textColor2 = value;
            }
        }

        /// <summary>
        /// 文本字体大小,以像素(pix)为单位,验证码图像大小根据此而变化，如果ImgSize大于由该值指定的大小，则图像大小为ImgSize
        /// </summary>
        [Bindable(false)]
        [Category("外观扩展"), Description("文本大小,以磅值为单位,验证码图像大小根据此而变化)"), NotifyParentProperty(true)]
        [DefaultValue(17)]
        [Localizable(false)]
        public int TextFontSize
        {
            get
            {
                return textFontSize;
            }

            set
            {
                textFontSize = value;
            }
        }

        /// <summary>
        /// 验证码图像大小,以像素(pix)为单位，如果TextFontSize指定的大小大于该值，则图像大小为TextFontSize指定的大小
        /// </summary>
        [Bindable(false), Browsable(false)]
        [Category("外观扩展"), NotifyParentProperty(true)]
        [Description("验证码图像大小,以像素(pix)为单位，如果TextFontSize指定的大小大于该值，则图像大小为TextFontSize指定的大小")]
        [Localizable(false)]
        public Size ImgSize
        {
            get
            {
                return imgSize;
            }

            set
            {
                imgSize = value;
            }
        }

        /// <summary>
        /// 验证码图像大小的宽度,以像素(pix)为单位，如果TextFontSize指定的大小大于该值，则图像大小为TextFontSize指定的大小
        /// </summary>
        [Bindable(false)]
        [Category("外观扩展"), NotifyParentProperty(true)]
        [Description("验证码图像大小的宽度,以像素(pix)为单位，如果TextFontSize指定的大小大于该值，则图像大小为TextFontSize指定的大小")]
        [DefaultValue(0)]
        public int Width
        {
            get
            {
                return imgSize.Width;
            }
            set
            {
                imgSize = new Size(value, imgSize.Height);
            }
        }

        /// <summary>
        /// 验证码图像大小的高度,以像素(pix)为单位，如果TextFontSize指定的大小大于该值，则图像大小为TextFontSize指定的大小
        /// </summary>
        [Bindable(false)]
        [Category("外观扩展"), NotifyParentProperty(true)]
        [Description("验证码图像大小的高度,以像素(pix)为单位，如果TextFontSize指定的大小大于该值，则图像大小为TextFontSize指定的大小")]
        [DefaultValue(0)]
        public int Height
        {
            get
            {
                return imgSize.Height;
            }
            set
            {
                imgSize = new Size(imgSize.Width, value);
            }
        }

    }

    /// <summary>
    /// ImageStyleType类型转换器
    /// </summary>
    public class ImageStyleTypeConver : ExpandableObjectConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(String))
                return true;
            else
                return base.CanConvertFrom(context, sourceType);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(string))
                return true;
            else
                return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            ImageStyle imgStyle = new ImageStyle();
            Dictionary<string, string> dic = new Dictionary<string, string>();

            if (value == null || !(value is string))
                return imgStyle;

            string valueStr = value as string;
            string[] values = valueStr.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < values.Length; i++)
            {
                if (values[i].Contains("="))
                    dic.Add(values[i].Substring(0, values[i].IndexOf("=")), values[i].Substring(values[i].IndexOf("=") + 1));
            }

            if (dic.ContainsKey("ImgBgColor")) imgStyle.ImgBgColor = Color.FromName(dic["ImgBgColor"]);
            if (dic.ContainsKey("ImgBorderColor")) imgStyle.ImgBorderColor = Color.FromName(dic["ImgBorderColor"]);
            if (dic.ContainsKey("ImgNoiseColor")) imgStyle.ImgNoiseColor = Color.FromName(dic["ImgNoiseColor"]);
            if (dic.ContainsKey("TextColor1")) imgStyle.TextColor1 = Color.FromName(dic["TextColor1"]);
            if (dic.ContainsKey("TextColor2")) imgStyle.TextColor2 = Color.FromName(dic["TextColor2"]);
            if (dic.ContainsKey("TextFontSize")) imgStyle.TextFontSize = int.Parse(dic["TextFontSize"]);
            if (dic.ContainsKey("Width")) imgStyle.Width = int.Parse(dic["Width"]);
            if (dic.ContainsKey("Height")) imgStyle.Height = int.Parse(dic["Height"]);

            return imgStyle;
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (value != null && !(value is ImageStyle))
                throw new Exception("错误的类型！");

            //字符类型
            if (destinationType == typeof(string))
            {
                if (value == null)
                    return string.Empty;

                ImageStyle imgStyle = value as ImageStyle;

                return "ImgBgColor=" + imgStyle.ImgBgColor.Name + "," +
                       "ImgBorderColor=" + imgStyle.ImgBorderColor.Name + "," +
                       "ImgNoiseColor=" + imgStyle.ImgNoiseColor.Name + "," +
                       "TextColor1=" + imgStyle.TextColor1.Name + "," +
                       "TextColor2=" + imgStyle.TextColor2.Name + "," +
                       "TextFontSize=" + imgStyle.TextFontSize +
                       "Width=" + imgStyle.Width +
                       "Height=" + imgStyle.Height;
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
}
